package com.team.ai.controller;

import com.team.ai.entity.Book;
import com.team.ai.entity.ResultEntity;
import com.team.ai.service.BookService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("v2/Book/")
public class BookController {
    private static final Logger logger = LoggerFactory.getLogger(BookController.class);//日志getLogger需要传入的类名

    @Resource
    BookService bookService;

    /**
     * 增
     * @param title
     * @param text
     * @return
     */
    @PostMapping("insert")
    private String insert(
            @RequestParam(value = "title",defaultValue = "-1") String title,
            @RequestParam(value = "img",defaultValue = "-1") String img,
            @RequestParam(value = "text",defaultValue = "-1") String text
    ){
        if (title.equals("")||text.equals("")){
            return ResultEntity.fail(-10002);
        }
        return bookService.insert(title,img ,text);
    }

    /**
     * 删
     * @param id
     * @return
     */
    @DeleteMapping("delete")
    private String delete(
            @RequestParam(value = "id",defaultValue = "-1") int id

            ){
        if (id==-1){
            return ResultEntity.fail(-10002);
        }
        return bookService.delete(id);
    }

    /**
     * 改
     */
    @PostMapping("update")
    private String update(
            @RequestParam(value = "title",defaultValue = "-1") String title,
            @RequestParam(value = "text",defaultValue = "-1") String text,
            @RequestParam(value = "img",defaultValue = "-1") String img,
            @RequestParam(value = "id",defaultValue = "-1") int id
    ){
        if (id==-1){
            return ResultEntity.fail(-10002);
        }
        return bookService.update(title, text,img, id);

    }

    /**
     * 查
     * @return
     */
    @GetMapping("selectList")
    private String selectList(

    ){
        return  bookService.selectList();

    }

    /**
     * 查询书本信息
     * @param id
     * @return
     */
    @GetMapping("selectDetail")
    private String selectDetail(
            @RequestParam(value = "id",defaultValue = "-1") int id

    ){
        if (id==-1){
            return ResultEntity.fail(-10002);
        }
        Book book=bookService.selectDetail(id);
        if( book!=null ){
            return ResultEntity.success(book);
        }else{
            return ResultEntity.fail(-10005);
        }
    }
}
